#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/11/19 19:37
# @Author : 闫峰
# @File    : es_utils.py
import requests
from requests.auth import HTTPBasicAuth

from Common.baserequest import Base_request
from Common.json_res import json_res


class Es_utils:


    def __init__(self,username,password,ip,port):
        """
        :param username:  鉴权账号
        :param password:  鉴权密码
        :param ip:  地址
        :param port:  端口
        """
        self.session = requests.Session()
        self.auth = HTTPBasicAuth(username,password)
        self.base_url = "http://{}:{}/".format(ip,port)

    def get_result(self,index,json):
        """

        :param index:  索引名
        :param json:   查询json
        :return:
        """
        url = self.base_url+index+"/_search"
        res = self.session.post(url=url,json=json,auth=self.auth)
        return res.json()










if __name__ == '__main__':

    canshu =("elastic","CoMHLgfpD7dxcA5dYFPK","10.158.158.165","9201")
    es = Es_utils(*canshu)
    json = {
    "query": {
        "bool": {
            "must": [
                {
                    "match_phrase": {
                        "ZhcxWordLabel": {
                            "query": "1"
                        }
                    }
                },
                {
                    "match_phrase": {
                        "GoogleWordLabel": {
                            "query": "1"
                        }
                    }
                },
                {
                    "match_phrase": {
                        "RiskLevel": {
                            "query": "1"
                        }
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 1000
}
    res = es.get_result(index="sensitive_wipo_distinctbrand_v1", json=json)
    res1 = json_res(res,r"$..Brand")
    print(res1)